<?php
namespace WebApper\Task;

class Task extends \WebApper\Base {

	/**
	 * The task id
	 *
	 * @var integer
	 */
	var $id;

	/**
	 * The subject line for the task
	 *
	 * @var string
	 */
	var $subject;

	/**
	 * The task due date timestamp
	 *
	 * @var integer
	 */
	var $due;

	/**
	 * The name of the task template used (if any)
	 *
	 * @var string
	 */
	var $task_type;

	/**
	 * The user ID of the task creator
	 *
	 * @var integer
	 */
	var $author_id;

	/**
	 * The user ID for who the task is for
	 *
	 * @var integer
	 */
	var $user_id;

	/**
	 * The description for the task
	 *
	 * @var string
	 */
	var $description;

	/**
	 * The status for the task
	 *
	 * @var string
	 */
	var $status;

	/**
	 * The post ID that the task is related to (if any)
	 *
	 * @var integer
	 */
	var $post_id;

	/**
	 * The date the task was created
	 *
	 * @var string
	 */
	var $created_on;

	/**
	 * The date the task was last edited
	 *
	 * @var string
	 */
	var $edited_on;


	/** Public Methods ********************************************************/

	/**
	 * Constructor
	 *
	 * @param integer $id
	 */
	function __construct( $id = 0 ) {
		if ( !empty( $id ) ) {
			$this->id = $id;
			$this->populate();
		}
	}

	/**
	 * Update or insert task details into the database.
	 *
	 * @global wpdb $wpdb WordPress database object
	 * @return bool Success or failure
	 */
	function save() {
		global $webapper, $wpdb;

		// Update
		if ( !empty( $this->id ) ) {
			$result = $wpdb->update( 
				$webapper->tables->tasks, 
				array( 
					'subject' => $this->subject,
					'due' => $this->due,
					'task_type' => $this->task_type,
					'user_id' => $this->user_id,
					'description' => $this->description,
					'status' => $this->status,
					'post_id' => $this->post_id,
				), 
				array( 'ID' => $this->id ), 
				array( '%s', '%d', '%s', '%d', '%s', '%s', '%d' ), 
				array( '%d' ) 
			);	

		// Insert
		} else {
			$result = $wpdb->insert( 
				$webapper->tables->tasks, 
				array( 
					'subject' => $this->subject,
					'due' => $this->due,
					'task_type' => $this->task_type,
					'author_id' => $this->author_id,
					'user_id' => $this->user_id,
					'description' => $this->description,
					'status' => $this->status,
					'post_id' => $this->post_id,
					'created_on' => current_time('mysql'),
				), 
				array( '%s', '%d', '%s', '%d', '%d', '%s', '%s', '%d', '%s' ) 
			);
			$this->id = $wpdb->insert_id;
		}
	
		if ( !$result )
			return false;

		return true;
	}

    /**
     * Archives a task
     *
     */
	public function archive() {
		global $webapper, $wpdb;

		$result = $wpdb->update( 
			$webapper->tables->tasks, 
			array( 
				'status' => 'archived',
			), 
			array( 'ID' => $this->id ), 
			array( '%s' ),
			array( '%d' )
		);	

		if ( !$result )
			return false;

		return true;
	}

	/** Private Methods *******************************************************/

	/**
	 * Fetches the task data from the database.
	 *
	 * @global $webapper
	 * @global wpdb $wpdb WordPress database object
	 */
	private function populate() {
		global $webapper, $wpdb;

		if ( $task = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$webapper->tables->tasks} WHERE ID = %d", $this->id ) ) ) {
			$this->id				= $task->ID;
			$this->subject			= $task->subject;
			$this->due				= $task->due;
			$this->charge_amount	= $task->charge_amount;
			$this->task_type		= $task->task_type;
			$this->author_id		= $task->author_id;
			$this->user_id			= $task->user_id;
			$this->description		= $task->description;
			$this->status			= $task->status;
			$this->post_id			= $task->post_id;
			$this->created_on		= $task->created_on;
			$this->edited_on		= $task->edited_on;
		}
	}

}

?>